﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class InventoryPopup : MonoBehaviour
{

    [SerializeField] private Image[] itemIcons;
    [SerializeField] private Text[] itemLabels;

    [SerializeField] private Text curItemLabel;
    [SerializeField] private Button equipButton;
    [SerializeField] private Button useButton;

    private string _curItem;

    public void Refresh()
    {
        List<string> itemList = Manager.Inventory.GetItemList();

        int len = itemIcons.Length;
        for (int i = 0; i < len; i++)
        {
            //当循环所有UI图像时检查仓库列表
            if (i<itemList.Count)
            {
                itemIcons[i].gameObject.SetActive(true);
                itemLabels[i].gameObject.SetActive(true);

                string item = itemList[i];

                //从Resources加载精灵
                Sprite sprite = Resources.Load<Sprite>("Icons/" + item);
                itemIcons[i].sprite = sprite;
                //重新设置图像的大小为精灵的原始大小
                itemIcons[i].SetNativeSize();

                int count = Manager.Inventory.GetItemCount(item);
                string message = "x" + count;
                //标签出来显示物品数量外，还可能显示"Equipped"
                if (item == Manager.Inventory.equippedItem)
                {
                    message = "Equipped\n" + message;
                }
                itemLabels[i].text = message;
                EventTrigger.Entry entry = new EventTrigger.Entry();
                //允许单击图标
                entry.eventID = EventTriggerType.PointerClick;
                //为每个物品触发不同的lambda函数
                entry.callback.AddListener((BaseEventData data) => { OnItem(item); });

                EventTrigger trigger = itemIcons[i].GetComponent<EventTrigger>();
                //清空侦听器，以便从空白状态刷新
                trigger.triggers.Clear();
                //将侦听器函数添加到EventTrigger
                trigger.triggers.Add(entry);
            }
            else
            {
                //如果没有物品需要显示，则隐藏这个图像/文本
                itemIcons[i].gameObject.SetActive(false);
                itemLabels[i].gameObject.SetActive(false);
            }
        }
        if (!itemList.Contains(_curItem))
        {
            _curItem = null;
        }
        //如果没有选择物品
        if (_curItem == null)
        {
            curItemLabel.gameObject.SetActive(false);
            equipButton.gameObject.SetActive(false);
            useButton.gameObject.SetActive(false);
        }
        else
        {
            //显示当前选择物品
            curItemLabel.gameObject.SetActive(true);
            equipButton.gameObject.SetActive(true);
            //仅使用按钮显示血量物品
            if (_curItem == "health")
            {
                useButton.gameObject.SetActive(true);
            }
            else
            {
                useButton.gameObject.SetActive(false);
            }

            curItemLabel.text = _curItem + ":";
        }
    }

    //被鼠标单击侦听器调用函数
    public void OnItem(string item)
    {
        _curItem = item;
        //改变物品后刷新仓库显示
        Refresh();
    }

    public void OnEquip()
    {
        Manager.Inventory.EquipItem(_curItem);
        Refresh();
    }

    public void OnUse()
    {
        Manager.Inventory.ConsumeItem(_curItem);
        if (_curItem == "health")
        {
            Manager.Player.ChangeHealth(25);
        }
        Refresh();
    }

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
